<?php

class LessionModel extends BaseModel {

    public function index() {
        $lession = $this->find_all("lession AS l LEFT JOIN subject AS s ON l.subject_id = s.subject_id", "lession_id ASC", "", "l.lession_id, l.lession_name, l.theory, l.lab, s.subject_name, l.status ", "");
        $this->viewModel->set("pageTitle", "Lession Manager");
        $this->viewModel->set("lession", $lession);
        return $this->viewModel;
    }

    public function create($data) {

        if (!empty($data)) {
            return $this->save("lession", $data);
        }

        $subjects = $this->find_all("subject", "subject_id ASC", "", "*");

        $this->viewModel->set("subjects", $subjects);
        $this->viewModel->set('pageTitle', 'Create Lession');
        return $this->viewModel;
    }

    public function edit($id, $data) {
        if (!empty($data)) {
            //return bool

            return $this->save($table = 'lession', $data = $data, $where = 'lession_id=' . $id);
        }

        //get item detail by id
        $lession = $this->find($table = 'lession AS l LEFT JOIN subject AS s ON l.subject_id = s.subject_id', $orderby = 'lession_id DESC', $where = 'l.lession_id=' . $id, $cols = 'l.lession_id, l.lession_name, l.theory, l.lab, l.status, l.subject_id ,s.subject_name', $limit = '1');
        //param var for view

        $this->viewModel->set("pageTitle", "Update User");
        $this->viewModel->set("lession", $lession);
        $sbj = $this->find_all("subject", "subject_id ASC", "", "*");
        $this->viewModel->set("sbjs", $sbj);
        return $this->viewModel;
    }

    public function getList($id) {

        $lessions = $this->find_all("lession AS l LEFT JOIN subject AS s ON l.subject_id = s.subject_id", " l.lession_id ASC ", "l.subject_id={$id} AND l.lession_name NOT LIKE '#@@#@#@#@#%' ", "l.lession_id, l.lession_name");
        $this->viewModel->set("lessionList", $lessions);
        return $this->viewModel;
    }

}
